#include "../Headers/types.h"

#include "ws.client.h"
#include "ws.request.h"
#include "ws.packer.h"
#include "ws.unpacker.h"

#include <iostream>
#include <cstring>
#include <map>

using namespace std;
using namespace TOB;
using namespace WS;
int main() {
  /*
  // test request parse
  cout << "input request: ";
  string in;
  cin >> in;
  
  Request* testReq = NULL;
  try {
    testReq = new Request(in);
  } catch (RequestExeption) {
    cout << "Parse error." << endl;
  }
  
  if (testReq != NULL) {
    cout << "Scheme: " << testReq->getScheme() << endl;
    cout << "Host: " << testReq->getHost() << endl;
    cout << "Path To File: " << testReq->getPathToFile() << endl;
    if (!testReq->getParams().empty()) {
      for (auto it = testReq->getParams().begin(); it != testReq->getParams().end(); it++) {
        cout << (*it).first << " => " << (*it).second << endl;
      }
    }
  }
  
  // create data
  string testBufferStr;
  cout << "Введите строку данных: ";
  cin >> testBufferStr;
  Data testData = Data((int8*)(testBufferStr.c_str()), testBufferStr.length());
  cout << "Created buffer: " << testData.getBuffer() << endl;
  
  // packer
  Packer* testPacker = NULL;
  try {
    testPacker = new Packer(in, &testData, 1488, "MVOvcharik", "Test");
  } catch (PackerExeption) {
    cout << "Packer error." << endl;
  }
  
  // unpacker
  Unpacker* testUnpacker = NULL;
  if (testPacker != NULL) {
    try {
      testUnpacker = new Unpacker(*testPacker);
    } catch (PackerExeption) {
      cout << "Unpacker error." << endl;
    }
  }
  
  if (testUnpacker != NULL) {
    cout << "Function: " << testUnpacker->getFunc() << endl;
    cout << "Request: " << testUnpacker->getRequest() << endl;
    cout << "User Name: " << testUnpacker->getUsername() << endl;
    cout << "Password: " << testUnpacker->getPassword() << endl;
    cout << "NSIP: " << testUnpacker->getNSIP() << endl;
    cout << "Data: " << testUnpacker->getData().getBuffer() << endl;
  }
  */
  string testRequest;
  cout << "Введите запрос: ";
  cin >> testRequest;
  
  string testBufferStr;
  cout << "Введите строку данных: ";
  cin >> testBufferStr;
  Data testData = Data((int8*)(testBufferStr.c_str()), testBufferStr.length());
  Client client = Client(1 << 24 | 127, "root", "123456");
  try {
    cout << "code: " << client.request(testRequest, testData) << endl;
  } catch (ClientException e) {
    cout << e.getMessage() << endl;
    e.writeErrorToLog();
    return e.getErrorCode();
  }
  
  cout << "Ответ сервера: " << testData.getData() << endl;
  
  return 0;
}
